package com.lark.view.splash;

import com.lark.view.SplashScreen;
import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.scene.Parent;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.SVGPath;
import javafx.util.Duration;

public class AnimationScreen extends SplashScreen {

    int w = 316;
    int h = 660;
    int radius = 80;

    StackPane iconPane;

    FadeTransition fadeTransition;

    public AnimationScreen() {
        super(true);
    }

    public AnimationScreen(boolean visible) {
        super(visible);
    }

    @Override
    public Parent getParent() {
        SVGPath icon = new SVGPath();
        icon.setScaleX(2);
        icon.setScaleY(2);
        icon.setContent("M10.3,7.3L8.2,5.2l0,0L7.8,4.5L7,3.9L6.1,4.8l0.6,0.8L7.4,6l0,0l2.1,2.1L9.2,8.4l1.4,1.4  c0.3,0.3,0.7,0.3,1,0L12,9.5c0.3-0.3,0.3-0.7,0-1l-1.4-1.4C10.6,7,10.3,7.3,10.3,7.3zM11.8,6.3c0.5-0.5,0.5-1.2,0.3-1.7l-0.9,1L10.6,5l0.9-1C11,3.7,10.3,3.8,9.8,4.3C9.4,4.6,9.2,5.2,9.4,5.7  L6.2,8.8C6.1,9,6.1,9.3,6.2,9.5l0,0l0.4,0.3c0.2,0.2,0.5,0.2,0.7,0l3.2-3.1C10.9,6.8,11.4,6.7,11.8,6.3zM1.4,2.4h1.4v7.5H1.4V2.4z M1.9,2.4h4.4v1.3H1.9C1.9,3.8,1.9,2.4,1.9,2.4z M1.9,5.6h3.8v1.3H1.9V5.6zM10.3,13H2.7C1.2,13,0,11.8,0,10.3V2.7C0,1.2,1.2,0,2.7,0h7.6C11.8,0,13,1.2,13,2.7v7.6C13,11.8,11.8,13,10.3,13z M2.7,0.3  c-1.3,0-2.4,1.1-2.4,2.4v7.6c0,1.3,1.1,2.4,2.4,2.4h7.6c1.3,0,2.4-1.1,2.4-2.4V2.7c0-1.3-1.1-2.4-2.4-2.4C10.3,0.3,2.7,0.3,2.7,0.3z");
        icon.setFill(new LinearGradient(
                0.0, 0.0, 1.0, 0.0, true, CycleMethod.NO_CYCLE,
                new Stop(0.0, new Color(0.11, 0.52, 0.93, 1.0)),
                new Stop(1.0, new Color(0.68, 0.05, 0.93, 1.0))));

        iconPane = new StackPane(icon);
        iconPane.setPrefSize(radius, radius);
        iconPane.setStyle("-fx-background-color: white;-fx-background-radius: " + radius / 2.0 + "px");
        iconPane.setEffect(new DropShadow());
        iconPane.setLayoutX((w - radius) / 2.0);
        iconPane.setLayoutY((h - radius) / 2.0);
        iconPane.setOpacity(0.3);

        Pane root = new Pane();
        root.setPrefSize(w, h);
        root.getChildren().add(iconPane);
        setAnimation();
        return root;
    }

    @Override
    public boolean visible() {
        return visible;
    }

    @Override
    public void stop() {
        fadeTransition.stop();
    }

    @Override
    public void setAnimation() {
        fadeTransition = new FadeTransition(Duration.seconds(1), iconPane);
        fadeTransition.setFromValue(0.3);
        fadeTransition.setToValue(1.0);
        fadeTransition.setAutoReverse(true);
        fadeTransition.setInterpolator(Interpolator.EASE_IN);
        fadeTransition.setCycleCount(Timeline.INDEFINITE);
        fadeTransition.play();
    }
}
